package com.zking.frame.dhy;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.sun.jndi.url.dns.dnsURLContext;
import com.zking.db.BaseDao;
import com.zking.entityClass.*;
import com.zking.frame.zc.UpdateMember;
import com.zking.utils.StringUtils;

public class BooksData extends JDialog implements TreeSelectionListener {
	{
		// 调用美化包
		BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
		try {
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 日期
	private String pattern = "yyyy-MM-dd";

	// 将int型转为string型
	public String doubleFormat(double number, String pattern) {
		DecimalFormat df = new DecimalFormat();
		df.applyPattern(pattern);
		return df.format(number);
	}

	// 北部
	private JPanel jp_north;
	private JButton jb_add;// 增加
	private JButton jb_export;// 导出
	private JButton jb_delete;// 删除
	private JButton jb_query;// 查询
	private JButton jb_modify;// 修改
	private JTextField tf_query;// 查询文本框
	private JLabel null_jl;

	// 中部
	private JPanel jp_center;
	// 将中部面板分割 分为左和右
	private JSplitPane jsplitPane;
	private JPanel jp_center_left;// 左边
	private JPanel jp_center_right;// 右边
	// 左边为树状列表
	private JTree tree;
	// 右边
	private DefaultTableModel contentArea;
	private JTable table;
	private String[][] data;
	private String[] header = new String[] { "书号", "书名", "出版社", "作者", "库存数量", "定价", "零售价", "会员价", "批发价", "成本价", "库存上限",
			"库存下限", "上架日期", "书的类型" };
	public List<BookInfo> booklist = new ArrayList<BookInfo>();

	// 组件图片

	private ImageIcon img_add = new ImageIcon(BooksData.class.getResource("22.png"));// 增加
	private ImageIcon img_export = new ImageIcon(BooksData.class.getResource("导出.png"));// 导出
	private ImageIcon img_delete = new ImageIcon(BooksData.class.getResource("44.png"));// 删除
	private ImageIcon img_query = new ImageIcon(BooksData.class.getResource("11.png"));// 查询
	private ImageIcon img_modify = new ImageIcon(BooksData.class.getResource("33.png"));// 修改

	private Border border = BorderFactory.createMatteBorder(0, 0, 3, 2, Color.gray);// 边框

	private static String className = "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel";// 美化包：main中调用

	private int selectRow;// 所选中的行
	private String sql;

	BaseDao base = new BaseDao();

	public BooksData() {

		setLayout(new BorderLayout());

		// 北部
		jp_north = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 5));
		// 增加
		jb_add = new JButton(img_add);
		jb_add.setBorder(border);
		jb_add.setSize(100, 100);

		jb_add.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				new addWindow(BooksData.this, 1, 1, 0);

			}
		});

		// 导出
		jb_export = new JButton(img_export);
		jb_export.setBorder(border);
		// 删除按钮
		jb_delete = new JButton(img_delete);
		jb_delete.setBorder(border);
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if ((selectRow = table.getSelectedRow()) != -1) {

					if (JOptionPane.showConfirmDialog(BooksData.this, "确认要删除吗？", "提示",
							JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
						sql = "update bookinfo set inventoryNums=0 where id=" + table.getValueAt(selectRow, 0);
					try {
						Connection conn = base.getConn();
						Statement st = conn.createStatement();
						int rs = st.executeUpdate(sql);
						JOptionPane.showMessageDialog(BooksData.this, "删除成功...");
						allsql();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(BooksData.this, "请选中所需删除的库单");
				}

			}
		});
		// 查询按钮
		jb_query = new JButton(img_query);
		jb_query.setBorder(border);
		jb_query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (tf_query.getText().equals("") || tf_query.getText().length() == 0) {
					JOptionPane.showMessageDialog(BooksData.this, "填写搜索关键词", "error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				String sql1 = "select * from bookinfo where type = ?";
				String sql2 = "select * from bookinfo where bookname = ?";
				List<Object> params = new ArrayList<>();
				List<Object[]> data = null;
				params.add(tf_query.getText());
				boolean flag1 = true;
				boolean flag2 = true;

				try {
					data = base.queryArrayListHandler(sql1, params.toArray());
					if (data.size() == 0) {
						flag1 = false;
						data = base.queryArrayListHandler(sql2, params.toArray());
						if (data.size() == 0) {
							flag2 = false;
							JOptionPane.showMessageDialog(BooksData.this, "没有此类书籍", "提示", JOptionPane.ERROR_MESSAGE);
							return;
						}
					}
					if (flag1 == true) {
						contentArea.setDataVector(null, header);// 清空面板
						needNature(sql1, params.toArray());
					} else if (flag2 == true && flag1 == false) {
						contentArea.setDataVector(null, header);// 清空面板
						needNature(sql2, params.toArray());
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		// 查询文本框
		tf_query = new JTextField("输入书名或者类型");
		tf_query.setPreferredSize(new Dimension(150, 30));
		tf_query.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				tf_query.setText("");
			}

			public void mousePressed(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
			}
		});

		// 修改按钮
		jb_modify = new JButton(img_modify);
		jb_modify.setBorder(border);
		jb_modify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if ((selectRow = table.getSelectedRow()) != -1) {
					int id = (int) table.getValueAt(selectRow, 0);
					new addWindow(BooksData.this, 2, id, selectRow);
				} else {
					JOptionPane.showMessageDialog(BooksData.this, "请选中所需修改的库单");
				}
			}
		});
		// 空的组件
		null_jl = new JLabel("                                                 " + "                                  "
				+ "                                             ");

		jb_add.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jb_delete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jb_query.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jb_modify.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

		jp_north.add(jb_add);
		jp_north.add(jb_delete);
		jp_north.add(tf_query);
		jp_north.add(jb_query);
		jp_north.add(jb_modify);
		jp_north.add(null_jl);
		jp_north.add(jb_export);

		// 中部
		jp_center = new JPanel(new BorderLayout());
		jp_center.setSize(700, 685);
		// 1.1分割面板
		jsplitPane = new JSplitPane();
		jp_center_left = new JPanel(new BorderLayout());
		jp_center_right = new JPanel(new BorderLayout());
		jsplitPane.setLeftComponent(jp_center_left);
		jsplitPane.setRightComponent(jp_center_right);
		// 分隔条上显示快速 折叠/展开 两边组件的小按钮
		jsplitPane.setOneTouchExpandable(true);
		// 拖动分隔条时连续重绘组件
		jsplitPane.setContinuousLayout(true);
		// 设置分隔条的初始位置
		jsplitPane.setDividerLocation(150);

		// 1.2左边面板
		// 创建根节点
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("所有类别");

		// 二级节点
		DefaultMutableTreeNode gdNode = new DefaultMutableTreeNode("书籍类");
		DefaultMutableTreeNode fjNode = new DefaultMutableTreeNode("其他");

		// 把二级节点作为子节点添加到根节点
		rootNode.add(gdNode);
		rootNode.add(fjNode);

		// 创建三级节点
		DefaultMutableTreeNode gzNode = new DefaultMutableTreeNode("教辅");
		DefaultMutableTreeNode szNode = new DefaultMutableTreeNode("文学");

		DefaultMutableTreeNode fzNode = new DefaultMutableTreeNode("地理");
		DefaultMutableTreeNode xmNode = new DefaultMutableTreeNode("考试");

		DefaultMutableTreeNode tbNode = new DefaultMutableTreeNode("历史");
		DefaultMutableTreeNode gxNode = new DefaultMutableTreeNode("音乐");
		DefaultMutableTreeNode jlNode = new DefaultMutableTreeNode("其他书籍");

		// 把三级节点作为子节点添加到相应的二级节点
		gdNode.add(gzNode);
		gdNode.add(szNode);

		gdNode.add(fzNode);
		gdNode.add(xmNode);

		gdNode.add(tbNode);
		gdNode.add(gxNode);
		fjNode.add(jlNode);

		// 用实现了TreeModel接口的DefaultTreeModel类来指定树的根结点
		DefaultTreeModel dtm = new DefaultTreeModel(rootNode);
		// 用这个根结点就可以去建立JTree树了
		tree = new JTree(dtm);
		tree.addTreeSelectionListener(this);// 为这棵树注册监听器

		// 设置树显示根节点句柄
		tree.setShowsRootHandles(true);

		// 设置树节点可编辑
		tree.setEditable(false);

		// 创建滚动面板，包裹树（因为树节点展开后可能需要很大的空间来显示，所以需要用一个滚动面板来包裹）
		JScrollPane scrollPane = new JScrollPane(tree);

		// 添加滚动面板到那内容面板
		jp_center_left.add(scrollPane, BorderLayout.CENTER);

		tree.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 如果在这棵树上点击了2次,即双击
				if (e.getSource() == tree && e.getClickCount() == 2) {
					// 按照鼠标点击的坐标点获取路径
					TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
					if (selPath != null)// 谨防空指针异常!双击空白处是会这样
					{
						// 获取这个路径上的最后一个组件,也就是双击的地方
						DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
						if (node.toString() == "所有类别") {
							allsql();
						}
					}
				}

			}

		});

		// 1.3右边面板
		// 表格
		contentArea = new DefaultTableModel(header, 0);
		table = new JTable(contentArea);
		table.getColumnModel().getColumn(12).setPreferredWidth(200);// 日期增宽
		JScrollPane sp_center_right = new JScrollPane(table);
		JTableHeader head = table.getTableHeader();
		head.setPreferredSize(new Dimension(head.getWidth(), 35));
		head.setFont(new Font("楷体", Font.BOLD, 15));
		table.setFont(new Font("宋体", Font.PLAIN, 17));
		sp_center_right.getViewport().setOpaque(false);
		sp_center_right.setBackground(Color.white);
		jp_center_right.add(sp_center_right);

		jp_center.add(jsplitPane);

		add(BorderLayout.NORTH, jp_north);
		add(BorderLayout.CENTER, jp_center);

		allsql();
		setTitle("图书资料");
		setSize(1425, 685);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(this);
		setVisible(true);
	}

	public static void main(String[] args) {

		new BooksData();
	}

	// 将所有数据加入面板中

	public void allsql() {
		contentArea.setDataVector(null, header);// 清空面板
		String sqlall = "SELECT * FROM bookinfo where inventoryNums>0";
		needNature(sqlall);
	}

	// 取出相应的字段
	public void needNature(String sql, Object... params) {
		try {
			booklist = base.findList(BookInfo.class, sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for (int i = 0; booklist != null && i < booklist.size(); i++) {
			// "书号", "书名", "出版社", "作者", "库存数量", "定价", "零售价", "会员价", "批发价", "成本价", "库存上限",
			// "库存下限", "出版日期", "书的类型"
			BookInfo books = booklist.get(i);
			Vector<Object> rowData = new Vector<>();
			rowData.add(books.getId());
			rowData.add(books.getBookName());
			rowData.add(books.getPublishHouse());
			rowData.add(books.getAuthor());
			rowData.add(books.getInventoryNums());
			rowData.add(books.getPrice());
			rowData.add(books.getRetailPrice());
			rowData.add(books.getVipPrice());
			rowData.add(books.getWholsalePrice());
			rowData.add(books.getCostPrice());
			rowData.add(books.getCountMax());
			rowData.add(books.getCountMin());
			rowData.add(books.getOpenDate());
			rowData.add(books.getType());
			contentArea.addRow(rowData);
		}

	}

	// 根据类别选择语句
	public void typeSql(String str) {
		String sql = "SELECT * FROM bookinfo WHERE TYPE = ?";
		contentArea.setDataVector(null, header);
		List<Object> params = new ArrayList<>();
		params.add(str);
		needNature(sql, params.toArray());
	}

	// 子节点点击事件
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		if (e.getSource() == tree) // 如果是要相应的那棵树
		{
			// 获取当前选择的第一个结点中的最后一个路径组件
			DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
			// 如果是叶子结点
			if (dmt.isLeaf()) {
				String str = dmt.toString();// 叶子结点的字符串
				// 判断
				if (str.equals("教辅")) {
					typeSql(str);
				} else if (str.equals("文学")) {
					typeSql(str);
				} else if (str.equals("地理")) {
					typeSql(str);
				} else if (str.equals("考试")) {
					typeSql(str);
				} else if (str.equals("历史")) {
					typeSql(str);
				} else if (str.equals("音乐")) {
					typeSql(str);
				} else {
					typeSql("其他书籍");
				}
			}

		}
	}

	/**
	 * 增加的子窗口
	 * 
	 */

	class addWindow extends JDialog implements KeyListener {

		// "书号","书名","出版社","作者","库存数量","定价","零售价","零售折扣","批发价","成本价"
		private static final long serialVersionUID = 1L;
		private JLabel jl_booknumber;// 书号
		private JTextField jtx_booknumber;// 书号文本框
		private JCheckBox jcb_discount;// 打折复选框
		private JLabel jl_bookname;// 书名
		private JTextField jtx_bookname;
		private JCheckBox jcb_member;// 会员折扣复选框
		private JLabel jl_bookType;// 书的类型
		private JComboBox<String> jcb_bookType;
		private JLabel jl_author;// 作者
		private JTextField jtx_author;
		private JLabel jl_press;// 出版社
		private JTextField jtx_press;
		private JLabel jl_bookcount;// 书的数量
		private JTextField jtx_bookcount;
		private JLabel jl_line;// 分割线
		private JLabel jl_costPrice;// 成本价
		private JTextField jtx_costPrice;
		private JLabel jl_price;// 定价
		private JTextField jtx_price;
		private JLabel jl_retailPrice;// 零售价
		private JTextField jtx_retailPrice;
		private JLabel jl_retailDiscount;// 零售折扣
		private JTextField jtx_retailDiscount;
		private JLabel jl_wholesale;// 批发价
		private JTextField jtx_wholesale;
		private JLabel jl_wholesaleDiscount;// 批发折扣
		private JTextField jtx_wholesaleDiscount;
		private JLabel jl_membershipPrice;// 会员价格
		private JTextField jtx_membershipPrice;
		private JLabel jl_countmax;// 库存最大数量
		private JTextField jtx_countmax;
		private JLabel jl_countmin;// 库存最少量
		private JTextField jtx_countmin;
		private boolean jcb_discountFlag = false;// 此商品出售时是否按本折扣
		private boolean jcb_memberFlag = false;// 此商品出售时是否按本折扣

		private JLabel jl_percentSign1;// 百分号
		private JLabel jl_percentSign2;// 百分号
		private JLabel jl_percentSign3;// 百分号

		// 颜色
		private Color blue = new Color(0x9FB6CD);

		private JButton jb_determine;// 确定按钮
		private JButton jb_cancel;// 取消按钮

		/**
		 * 
		 * @param owner
		 * @param type  1为添加，2为修改
		 */
		public addWindow(JDialog owner, int type, int id, int selectRow) {
			super(owner, true);
			setLayout(null);
//			setForeground(blue);
//			setBackground(blue);

			jl_percentSign1 = new JLabel("%");
			jl_percentSign1.setBounds(810, 285, 50, 50);
			jl_percentSign1.setFont(new Font("楷体", Font.BOLD, 28));
			jl_percentSign1.setForeground(Color.red);

			jl_percentSign2 = new JLabel("%");
			jl_percentSign2.setBounds(810, 345, 50, 50);
			jl_percentSign2.setFont(new Font("楷体", Font.BOLD, 28));
			jl_percentSign2.setForeground(Color.red);

			jl_percentSign3 = new JLabel("%");
			jl_percentSign3.setBounds(810, 405, 50, 50);
			jl_percentSign3.setFont(new Font("楷体", Font.BOLD, 28));
			jl_percentSign3.setForeground(Color.red);

			jl_booknumber = new JLabel("书号:");
			jl_booknumber.setFont(new Font("楷体", Font.BOLD, 28));
			jl_booknumber.setBounds(120, 20, 100, 50);

			jtx_booknumber = new JTextField("不可手动编辑");
			jtx_booknumber.setFont(new Font("楷体", Font.BOLD, 25));
			jtx_booknumber.setBounds(220, 30, 330, 40);
			jtx_booknumber.setEditable(false);

			jcb_discount = new JCheckBox("此商品出售时按本折扣");
			jcb_discount.setFont(new Font("楷体", Font.BOLD, 22));
			jcb_discount.setBounds(550, 20, 330, 50);
			jcb_discount.setOpaque(false);
			jcb_discount.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {

					JCheckBox checkBox = (JCheckBox) e.getSource();
					if (checkBox.isSelected() == true) {
						jcb_discountFlag = true;
					}

				}
			});

			jl_bookname = new JLabel("书名:");
			jl_bookname.setFont(new Font("楷体", Font.BOLD, 28));
			jl_bookname.setBounds(120, 70, 100, 50);

			jtx_bookname = new JTextField();
			jtx_bookname.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_bookname.setBounds(220, 80, 330, 30);

			jcb_member = new JCheckBox("此商品会员可打折");
			jcb_member.setFont(new Font("楷体", Font.BOLD, 22));
			jcb_member.setBounds(550, 70, 330, 50);
			jcb_member.setOpaque(false);
			jcb_member.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {

					JCheckBox checkBox = (JCheckBox) e.getSource();
					if (checkBox.isSelected() == true) {
						jcb_memberFlag = true;
					}

				}
			});

			jl_bookType = new JLabel("类型:");
			jl_bookType.setFont(new Font("楷体", Font.BOLD, 28));
			jl_bookType.setBounds(90, 125, 100, 50);

			jcb_bookType = new JComboBox<String>();
			String[] test = { "教辅", "文学", "地理", "考试", "历史", "音乐", "其他书籍" };
			for (int i = 0; i < test.length; i++) {
				jcb_bookType.addItem(test[i]);
			}
			jcb_bookType.setBounds(190, 130, 210, 40);
			jcb_bookType.setFont(new Font("楷体", Font.BOLD, 28));

			jl_author = new JLabel("作者:");
			jl_author.setFont(new Font("楷体", Font.BOLD, 28));
			jl_author.setBounds(470, 125, 100, 50);

			jtx_author = new JTextField();
			jtx_author.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_author.setBounds(550, 130, 250, 40);

			jl_press = new JLabel("出版社:");
			jl_press.setFont(new Font("楷体", Font.BOLD, 28));
			jl_press.setBounds(470, 185, 150, 50);

			jtx_press = new JTextField();
			jtx_press.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_press.setBounds(580, 190, 220, 40);

			jl_bookcount = new JLabel("期初库存数量:");
			jl_bookcount.setFont(new Font("楷体", Font.BOLD, 28));
			jl_bookcount.setBounds(90, 185, 250, 50);

			jtx_bookcount = new JTextField();
			jtx_bookcount.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_bookcount.setBounds(290, 190, 110, 40);

			jl_line = new JLabel(
					"------------------------------------------------------------------------------------------------");
			jl_line.setBounds(80, 180 + 60, 1000, 40);
			jl_line.setForeground(Color.red);

			jl_costPrice = new JLabel("成本价:");
			jl_costPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jl_costPrice.setBounds(90, 225 + 60, 120, 50);

			jtx_costPrice = new JTextField();
			jtx_costPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_costPrice.setBounds(200, 230 + 60, 200, 40);

			jl_price = new JLabel("定价:");
			jl_price.setFont(new Font("楷体", Font.BOLD, 28));
			jl_price.setBounds(470, 225 + 60, 100, 50);

			jtx_price = new JTextField();
			jtx_price.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_price.setBounds(550, 230 + 60, 250, 40);

			jl_retailPrice = new JLabel("零售价:");
			jl_retailPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jl_retailPrice.setBounds(90, 285 + 60, 120, 50);

			jtx_retailPrice = new JTextField();
			jtx_retailPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_retailPrice.setBounds(200, 290 + 60, 200, 40);

			jl_retailDiscount = new JLabel("零售折扣:");
			jl_retailDiscount.setFont(new Font("楷体", Font.BOLD, 28));
			jl_retailDiscount.setBounds(470, 285 + 60, 150, 50);

			jtx_retailDiscount = new JTextField("0");
			jtx_retailDiscount.setEnabled(false);
			jtx_retailDiscount.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_retailDiscount.setBounds(610, 290 + 60, 190, 40);

			jl_wholesale = new JLabel("批发价:");
			jl_wholesale.setFont(new Font("楷体", Font.BOLD, 28));
			jl_wholesale.setBounds(90, 345 + 60, 120, 50);

			jtx_wholesale = new JTextField();
			jtx_wholesale.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_wholesale.setBounds(200, 350 + 60, 200, 40);

			jl_wholesaleDiscount = new JLabel("批发折扣:");
			jl_wholesaleDiscount.setFont(new Font("楷体", Font.BOLD, 28));
			jl_wholesaleDiscount.setBounds(470, 345 + 60, 150, 50);

			jtx_wholesaleDiscount = new JTextField("80");
			jtx_wholesaleDiscount.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_wholesaleDiscount.setBounds(610, 350 + 60, 190, 40);
			jtx_wholesaleDiscount.setEnabled(false);

			jl_membershipPrice = new JLabel("会员售价:");
			jl_membershipPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jl_membershipPrice.setBounds(230, 415 + 60, 200, 50);

			jtx_membershipPrice = new JTextField();
			jtx_membershipPrice.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_membershipPrice.setBounds(370, 415 + 60, 250, 40);

			jl_countmin = new JLabel("库存下限:");
			jl_countmin.setFont(new Font("楷体", Font.BOLD, 28));
			jl_countmin.setBounds(90, 495 + 60, 150, 50);

			jtx_countmin = new JTextField();
			jtx_countmin.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_countmin.setBounds(230, 500 + 60, 170, 40);

			jl_countmax = new JLabel("库存上限:");
			jl_countmax.setFont(new Font("楷体", Font.BOLD, 28));
			jl_countmax.setBounds(470, 495 + 60, 150, 50);

			jtx_countmax = new JTextField();
			jtx_countmax.setFont(new Font("楷体", Font.BOLD, 28));
			jtx_countmax.setBounds(610, 500 + 60, 190, 40);

			jb_determine = new JButton("确定");
			jb_determine.setFont(new Font("楷体", Font.BOLD, 28));
			jb_determine.setBounds(650, 700, 190, 40);
			jb_determine.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					if (type == 2) {
						if (getMemberInfoData(type, id, true, true)) {
						allsql();
						dispose();
						}
					} else {
						if (getMemberInfoData(type, id, true, true)) {
							allsql();
							dispose();
						}
					}

				}
			});

			jb_cancel = new JButton("取消");
			jb_cancel.setFont(new Font("楷体", Font.BOLD, 28));
			jb_cancel.setBounds(70, 700, 190, 40);
			jb_cancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();

				}
			});

			// add(jl_percentSign1);
			add(jl_percentSign2);
			add(jl_percentSign3);

			add(jl_booknumber);
			add(jtx_booknumber);
			add(jcb_discount);
			add(jl_bookname);
			add(jtx_bookname);
			add(jcb_member);
			add(jl_bookType);
			add(jcb_bookType);
			add(jtx_author);
			add(jl_author);
			add(jl_press);
			add(jtx_press);
			add(jl_bookcount);
			add(jtx_bookcount);
			add(jl_line);
			add(jl_costPrice);
			add(jtx_costPrice);
			add(jtx_price);
			add(jl_price);
			add(jl_retailPrice);
			add(jtx_retailPrice);
			add(jtx_retailDiscount);
			add(jl_retailDiscount);
			add(jl_wholesale);
			add(jtx_wholesale);
			add(jl_wholesaleDiscount);
			add(jtx_wholesaleDiscount);
			add(jl_membershipPrice);
			add(jtx_membershipPrice);
			add(jl_countmin);
			add(jtx_countmin);
			add(jl_countmax);
			add(jtx_countmax);
			add(jb_cancel);
			add(jb_determine);

			if (type == 2) {// 添加默认数据
				updateGoodsInital(selectRow);
			}

			jtx_price.addKeyListener(this);
			getContentPane().setBackground(blue);
			setTitle("图书基本资料-定义");
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setSize(new Dimension(910, 850));
			setLocationRelativeTo(owner);
			setVisible(true);
		}

		public boolean getMemberInfoData(int type, int id, boolean flag1, boolean flag2) {
			try {

				String isEveryDiscount = "否";
				String isMemberDiscount = "否";
				if (jcb_discountFlag) {
					isEveryDiscount = "是";
				}
				if (jcb_memberFlag) {
					isMemberDiscount = "是";
				}
				String bookname = jtx_bookname.getText();//
				String press = jtx_press.getText();//
				String author = jtx_author.getText();
				String bookType = (String) jcb_bookType.getSelectedItem();
				String bookcount = doubleFormat(Double.parseDouble(jtx_bookcount.getText()), "000");
				String price = doubleFormat(Double.parseDouble(jtx_price.getText()), "00.0");
				String retailPrice = doubleFormat(Double.parseDouble(jtx_retailPrice.getText()), "00.0");
				String membershipPrice = doubleFormat(Double.parseDouble(jtx_membershipPrice.getText()), "00.0");
				String costPrice = doubleFormat(Double.parseDouble(jtx_costPrice.getText()), "00.0");
				String wholesale = doubleFormat(Double.parseDouble(jtx_wholesale.getText()), "00.0");
				String countmax = doubleFormat(Double.parseDouble(jtx_countmax.getText()), "000");
				String countmin = doubleFormat(Double.parseDouble(jtx_countmin.getText()), "000");

				if (bookname == "" || bookname.length() == 0 || press == "" || press.length() == 0 || author == ""
						|| author.length() == 0) {

					JOptionPane.showMessageDialog(addWindow.this, "请将数据填写完整");
					flag1 = false;
					return false;

				}

				if (type == 1) {

					String sameNatureSql = "SELECT * FROM bookinfo";
					List<Object> data = base.queryColumnListHandler(sameNatureSql, "bookname");
					List<Object> data2 = base.queryColumnListHandler(sameNatureSql, "author");
					for (int i = 0; i < data.size(); i++) {
						if (data.get(i).equals(bookname) || data2.get(i).equals(bookType)) {
							JOptionPane.showMessageDialog(addWindow.this, "添加书籍已存在。", "提示", JOptionPane.ERROR_MESSAGE);
							flag1 = false;
						}
					}

					Object[] params = { null, bookname, author, price, bookcount, null, bookType, press, retailPrice,
							membershipPrice, wholesale, countmax, countmin,
							StringUtils.dateToString(new Date(), pattern), costPrice, isEveryDiscount, isMemberDiscount,
							bookcount, 0.85, 0.8 };

					String sql = "insert into bookinfo values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// 不指定，则全部插入
					base.update(sql, params);// 返回影响行数
					dispose();
				} else if (type == 2) {

					String sql = "update bookinfo set bookname='" + bookname + "',author='" + author + "',price='"
							+ price + "',inventoryNums='" + bookcount + "',bookStoreId=null,type='" + bookType
							+ "',publishHouse='" + press + "',retailPrice='" + retailPrice + "',vipPrice='"
							+ membershipPrice + "',wholsalePrice='" + wholesale + "',countMax='" + countmax
							+ "',countMin='" + countmin + "',openDate='" + StringUtils.dateToString(new Date(), pattern)
							+ "',costPrice='" + costPrice + "',isEveryDiscount='" + isEveryDiscount
							+ "',isMemberDiscount='" + isMemberDiscount + "'where id=" + id;

					Connection conn;
					try {
						conn = base.getConn();
						Statement st = conn.createStatement();
						int rs = st.executeUpdate(sql);
					} catch (SQLException e) {
						e.printStackTrace();
					}

				}
				// JOptionPane.showMessageDialog(addWindow.this, "请输入完整的数据或正确的数字。", "提示",
				// JOptionPane.ERROR_MESSAGE);

			} catch (Exception e) {
				JOptionPane.showMessageDialog(addWindow.this, "请输入完整的数据或正确的数字。");
				flag2 = false;
			}
			if (flag1 == true && flag2 == true) {
				return true;
			} else {
				return false;
			}
		}

		/**
		 * 修改面板，填上来原数据
		 */
		public void updateGoodsInital(int selectRow) {
			int start = 1;
			jtx_bookname.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_press.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_author.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_bookcount.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_price.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_retailPrice.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_membershipPrice.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_costPrice.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_wholesale.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_countmax.setText(contentArea.getValueAt(selectRow, start++).toString());
			jtx_countmin.setText(contentArea.getValueAt(selectRow, start++).toString());
			jcb_bookType.setSelectedItem(contentArea.getValueAt(selectRow, start + 1).toString());

		}

		@Override
		public void keyTyped(KeyEvent e) {
			try {
				if ((int) e.getKeyChar() == 8) {

					String s = jtx_price.getText();
					if (s.length() == 0) {
						jtx_membershipPrice.setText("");
						jtx_wholesale.setText("");
						jtx_retailPrice.setText("");
					}
					double a = new Double(s.substring(0, s.length())) * 0.85;
					System.out.println(a);
					double b = new Double(s.substring(0, s.length())) * 0.8;
					System.out.println(b);
					jtx_membershipPrice.setText(doubleFormat(a, "00.00"));
					jtx_wholesale.setText(doubleFormat(b, "00.00"));
					jtx_retailPrice.setText(s.substring(0, s.length()));

				} else if ((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57) {
					String s = jtx_price.getText();
					double a = new Double(s.substring(0, s.length())) * 8.5;
					double b = new Double(s.substring(0, s.length())) * 8;
					jtx_membershipPrice.setText(doubleFormat(a, "00.00"));
					jtx_wholesale.setText(doubleFormat(b, "00.00"));
					jtx_retailPrice.setText(jtx_price.getText() + e.getKeyChar());
				} else {

					JOptionPane.showMessageDialog(addWindow.this, "请输入数字。", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}

			} catch (Exception e2) {
				// TODO: handle exception
				jtx_price.setText("");
			}

		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyReleased(KeyEvent e) {

		}

	}

}
